.TH std::pair::swap 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::pair::swap \- std::pair::swap

.SH Synopsis
   void swap( pair& other ) noexcept(/* see below */);                    \fI(since C++11)\fP
                                                                          \fI(until C++20)\fP
   constexpr void swap( pair& other ) noexcept(/* see below */);  \fB(1)\fP     \fI(since C++20)\fP
   constexpr void swap( const pair& other ) const noexcept(/* see     \fB(2)\fP (since C++23)
   below */);

   Swaps first with other.first and second with other.second, as if by using std::swap;
   swap(first, other.first); swap(second, other.second);.

   If either selected swap function call is ill-formed or does not swap   (until C++23)
   the value of the member, the behavior is undefined.
   1) The program is ill-formed if either std::is_swappable_v<T1> or
   std::is_swappable_v<T2> is not true.
   2) The program is ill-formed if either std::is_swappable_v<const T1>
   or std::is_swappable_v<const T2> is not true.                          (since C++23)

   If either selected swap function call does not swap the value of the
   member, the behavior is undefined.

.SH Parameters

   other - pair of values to swap

.SH Return value

   \fI(none)\fP

.SH Exceptions

   noexcept specification:
   noexcept(

        noexcept(swap(first, other.first)) &&
        noexcept(swap(second, other.second))                              \fI(until C++17)\fP

   )

   In the expression above, the identifier swap is looked up in the same
   manner as the one used by the C++17 std::is_nothrow_swappable trait.
   1)
   noexcept specification:
   noexcept(

        std::is_nothrow_swappable_v<first_type> &&
        std::is_nothrow_swappable_v<second_type>

   )                                                                      \fI(since C++17)\fP
   2)
   noexcept specification:
   noexcept(

        std::is_nothrow_swappable_v<const first_type> &&
        std::is_nothrow_swappable_v<const second_type>

   )

.SH Example


// Run this code

 #include <iostream>
 #include <utility>
 #include <string>
 int main()
 {
     std::pair<int, std::string> p1(10, "test"), p2;
     p2.swap(p1);
     std::cout << "(" << p2.first << ", " << p2.second << ")\\n";

 #if __cpp_lib_ranges_zip >= 202110L
     // Using the C++23 const qualified swap overload
     // (swap is no longer propagating pair constness)
     int i1 = 10, i2{};
     std::string s1("test"), s2;
     const std::pair<int&, std::string&> r1(i1, s1), r2(i2, s2);
     r2.swap(r1);
     std::cout << "(" << i2 << ", " << s2 << ")\\n";
 #endif
 }

.SH Possible output:

 (10, test)
 (10, test)

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

      DR    Applied to          Behavior as published           Correct behavior
   LWG 2456 C++11      the noexcept specification is ill-formed made to work

.SH See also

   swap swaps the values of two objects
        \fI(function template)\fP
   swap swaps the contents of two tuples
        \fI(public member function of std::tuple<Types...>)\fP

.SH Category:
     * conditionally noexcept
